/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "meteorshower.h"

#include <math.h>

/*! \class MeteorShower
    Meteor shower game object
*/

/*! \def NUM_METEORS
  Number of meteors at the meteor shower
*/
#define NUM_METEORS     4

/*!
  Constructs meteor shower.

  \a frame Current frame number. When the GameObject was created.

  \a rewindEnabled Is the rewind mode enabled.

  \a startTime Start time of meteor shower drawing.

  \a startRadius Start radius of the meteor shower.

  \a radiusChange Change of radiusChange when flying by.
*/
MeteorShower::MeteorShower(int frame, bool rewindEnabled, FPtype startTime, FPtype startRadius, 
                           FPtype radiusChange) 
    : GameObject(TypeMeteorShower, frame, startRadius, SpeedLimitHigh, rewindEnabled, MassMedium,
                 ScoreMeteorShower)
    , startTime(startTime)
    , startRadius(startRadius)
    , radiusChange(radiusChange)
    , centerX(0.0f)
    , centerY(0.0f)
    , vectorCenterToStartX(+1.0f)
    , vectorCenterToStartY(+1.0f)
{
    relativePositionX = new FPtype[NUM_METEORS];
    relativePositionY = new FPtype[NUM_METEORS];
    angularVelocity = new FPtype[NUM_METEORS];

    vertices = new GLfloat[NUM_METEORS * 9];
    colors = new GLfloat[NUM_METEORS * 9];

    for (int i = 0;i < NUM_METEORS;i++) {
        FPtype dist = 0.50f;
        dist += 0.50f * ((FPtype) rand() / (FPtype) RAND_MAX);

        FPtype angle = (FPtype) i * 2.0f * M_PI / (FPtype) NUM_METEORS;
        angle += 0.5f * M_PI * ((FPtype) rand() / (FPtype) RAND_MAX) / (FPtype) NUM_METEORS;

        relativePositionX[i] = dist * sin(angle);
        relativePositionY[i] = dist * cos(angle);

        angularVelocity[i] = 10.0f * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        angularVelocity[i] += (angularVelocity[i] < 0.0f ? -3.0f : +3.0f);

        // vertices are computed only in animate().

        GLfloat baseColor;
        const GLfloat baseRange = 0.50f;
        const GLfloat randomRange = 0.30f;

        baseColor = 0.60f + baseRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 0] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 1] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 2] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);

        baseColor = 0.60f + baseRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 3] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 4] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 5] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);

        baseColor = 0.60f + baseRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 6] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 7] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
        colors[i * 9 + 8] = baseColor + randomRange * (((FPtype) rand() / (FPtype) RAND_MAX) - 0.5f);
    }
}

/*!
  Destructor of the meteor shower.
*/
MeteorShower::~MeteorShower()
{
    if (relativePositionX != NULL) {
        delete[] relativePositionX;
        relativePositionX = NULL;
    }

    if (relativePositionY != NULL) {
        delete[] relativePositionY;
        relativePositionY = NULL;
    }

    if (angularVelocity != NULL) {
        delete[] angularVelocity;
        angularVelocity = NULL;
    }

    if (vertices != NULL) {
        delete[] vertices;
        vertices = NULL;
    }

    if (colors != NULL) {
        delete[] colors;
        colors = NULL;
    }
}

/// Gets radius of the meteor shower at \a currentTime.
FPtype MeteorShower::getRadius(FPtype currentTime) const
{
    const FPtype timeDiff = currentTime - startTime;
    if (timeDiff < 0.0f)
        return startRadius;
    else
        return startRadius + radiusChange * timeDiff;
}

/// Stores radius of the meteor shower at \a currentTime.
void MeteorShower::storeRadius(FPtype currentTime)
{
    GameObject::radius = getRadius(currentTime);
}

/// Animates the meteor shower at \a currentTime.
void MeteorShower::animate(FPtype currentTime)
{
    const GLfloat rad = getRadius(currentTime);
    const FPtype timeDiff = currentTime - startTime;

    const GLfloat meteorZ = 0.0f;
    const GLfloat meteorSize = 5.0f;
    const GLfloat offset2nd = 120.0f * M_PI / 180.0f;
    const GLfloat offset3rd = 240.0f * M_PI / 180.0f;

    for (int i = 0;i < NUM_METEORS;i++) {
        const GLfloat meteorX = relativePositionX[i] * rad;
        const GLfloat meteorY = relativePositionY[i] * rad;

        const GLfloat angle = angularVelocity[i] * timeDiff;

        vertices[i * 9 + 0] = meteorX + meteorSize * sin(angle);
        vertices[i * 9 + 1] = meteorY + meteorSize * cos(angle);
        vertices[i * 9 + 2] = meteorZ;

        vertices[i * 9 + 3] = meteorX + meteorSize * sin(angle + offset2nd);
        vertices[i * 9 + 4] = meteorY + meteorSize * cos(angle + offset2nd);
        vertices[i * 9 + 5] = meteorZ;

        vertices[i * 9 + 6] = meteorX + meteorSize * sin(angle + offset3rd);
        vertices[i * 9 + 7] = meteorY + meteorSize * cos(angle + offset3rd);
        vertices[i * 9 + 8] = meteorZ;
    }
}

/// Gets vertex array for meteors
GLfloat * MeteorShower::vertexArray()
{
    return vertices;
}

/// Gets color array for meteors
GLfloat * MeteorShower::colorArray()
{
    return colors;
}

/// Gets number of vertixes for meteors
int MeteorShower::numVertices() const
{
    return NUM_METEORS * 3;
}

/// Sets positions for meteor shover
void MeteorShower::setStartAndCenterPositions(const FPtype *start, const FPtype *center)
{
    centerX = center[0];
    centerY = center[1];

    vectorCenterToStartX = start[0] - center[0];
    vectorCenterToStartY = start[1] - center[1];
}

/// Has the meteor shower passed the center point of it's course.
bool MeteorShower::centerPointPassed()
{
    const GLfloat tmpX = posX() - centerX;
    const GLfloat tmpY = posY() - centerY;

    const GLfloat scalarProduct = tmpX * vectorCenterToStartX + tmpY * vectorCenterToStartY;

    // if the angle between vectors is > 90 degrees,
    // then the MeteorShower has passed it's center point.

    return (scalarProduct < 0.0f);
}
